﻿<!DOCTYPE html>
<html>
<head>
    <title></title>

    <script type="text/javascript">
        (function () {
            'use strict';

            function sendMessage(type, data) {
                window.parent.postMessage({ type: type, data: data, sender: 'mcnext.dynamicscripts' }, '*');
            }

            function applyScript(script, data) {
                var context = {
                    info: function (text, data) {
                        this.result.infos.push({ text: text, data: data });
                    },
                    alert: function (text, data) {
                        this.result.alerts.push({ text: text, data: data });
                    },
                    error: function (text, data) {
                        this.result.errors.push({ text: text, data: data });
                    },
                    result: {
                        infos: [],
                        alerts: [],
                        errors: []
                    }
                };

                for (var prop in data) {
                    context[prop] = data[prop];
                }

                eval(script);
                return context.result;
            }

            function receiveMessage(arg) {
                if (arg.data.type == 'applyscript') {
                    try {
                        var res = applyScript(arg.data.data.script, arg.data.data.data);
                        sendMessage('applyscript', { session: arg.data.data.session, data: res });
                    } catch (exception) {
                        sendMessage('applyscript', { session: arg.data.data.session, error: { description: exception.description, message: exception.message, stack: exception.stack } });
                    }
                } else if (arg.data.type == 'initialisation') {
                    sendMessage('initialized');
                }
            }

            var e = document.domain;
            window.addEventListener('message', receiveMessage);
        })();
    </script>
</head>
<body>
</body>
</html>